package com.okonna.selenium.tide;

import java.io.*;
import java.util.Arrays;

import org.eclipse.swt.widgets.*;

import com.okonna.selenium.tide.views.*;


public class Launcher
{
    private static boolean debug = false;

    public static void main(String[] args)
    {
		System.out.println("Launcher.main(" +  Arrays.toString(args) + ")");
		for (String arg : args)
		{
		    if (arg.equals("-debug"))
		    {
		    	setDebug(true);
		    }
		}
		
		if (getDebug()==false)
		{
		    try
		    {
		    	System.out.println("Not in debug mode, see app.log for further details or start again with -debug.");
		    	//TODO Refactor to use commons logging and log4j
		    	System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("app.log"))));
		    }
		    catch (Exception e)
		    {
		    	System.out.println("\nException:\n" + e + "\n");
		    }
		}
	
		try
		{
	
		    final Display display = new Display();
		    final Shell shMainShell;
		    final Application app = new Application();
		    app.setDisplay(display);
		    MainView swtMainView = new MainView(app);
		    //SandboxView swtMainView = new SandboxView(app);
		    shMainShell = swtMainView.initView();
		    app.startSession();
		    shMainShell.open();
		    while (!shMainShell.isDisposed())
		    {
				if (!display.readAndDispatch())
				{
				    display.sleep();
				}
		    }
		    display.dispose();
		    app.stopSession();
		}
		catch (Exception e)
		{
		    System.out.println("\nException:\n" + e + "\n");
		}
		return;
    }

    public static void setDebug(boolean tmpdebug)
    {
		System.out.println("Launcher.setDebug(" + tmpdebug + ")");
		debug = tmpdebug;
		return;
    }

    public static boolean getDebug()
    {
		System.out.println("Launcher.getDebug()");
		return debug;
    }
}
